package com.googlecode.maurersmarbles.drawables;

import com.googlecode.maurersmarbles.Board;
import com.googlecode.maurersmarbles.GameLogic;

import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;

public abstract class BoardDrawable implements Drawable {

	/**
	 * The board for this drawable.
	 */
	private Board board;
	/**
	 * The array that contains the player colors.
	 */
	private int[] playerColors;

	/**
	 * Creates a new BoardDrawable.
	 * @param board the board to draw
	 * @param playerColors the array that contains the player colors
	 */
	public BoardDrawable(Board board, int[] playerColors) {
		this.board = board;
		this.playerColors = playerColors;
	}

	@Override
	public void draw(Canvas canvas) {

		int spaceSize = board.getSpaceSize();

		// draw the board background
		Rect outerRect = board.getOuterRect();
		drawBackground(canvas, outerRect.left, outerRect.top,
				outerRect.width(), outerRect.height());

		// draw the empty spaces on the board
		for (int i = 0; i < Board.BOARD_SPACES; i++) {
			drawEmptySpace(canvas, board.getStartXYForGridCell(
					board.getGridCellForIndex(i), spaceSize), spaceSize,
					spaceSize);
		}

		// draw the safe spaces on the board
		for (int i = 0; i < GameLogic.NUMBER_OF_PLAYERS; i++) {
			drawSafeSpace(canvas, board.getStartXYForGridCell(
					board.getGridCellForIndex(Board.SAFE[i]), spaceSize),
					spaceSize, spaceSize, playerColors[i]);
		}

		// draw the home spaces on the board
		for (int i = 0; i < GameLogic.NUMBER_OF_PLAYERS; i++) {
			for (int y = Board.HOME_MIN[i]; y <= Board.HOME_MAX[i]; y++) {
				drawHomeSpace(
						canvas,
						board.getStartXYForGridCell(
								board.getGridCellForIndex(y), spaceSize),
						spaceSize, spaceSize, playerColors[i]);
			}
		}

		// draw the start spaces on the board
		for (int i = 0; i < GameLogic.NUMBER_OF_PLAYERS; i++) {
			for (int y = Board.START_MIN[i]; y <= Board.START_MAX[i]; y++) {
				drawStartSpace(
						canvas,
						board.getStartXYForGridCell(
								board.getGridCellForIndex(y), spaceSize),
						spaceSize, spaceSize, playerColors[i]);
			}
		}

		// draw the discard rectangle
		Rect discardRect = board.getDiscardRect();
		drawDiscardRectangle(canvas, discardRect.left, discardRect.top,
				discardRect.width(), discardRect.height());

	}

	/**
	 * Draws an empty space on the board.
	 * @param canvas the canvas to draw in
	 * @param startXY the start x/y location
	 * @param width the width of the space
	 * @param height the height of the space
	 */
	public void drawEmptySpace(Canvas canvas, Point startXY, int width,
			int height) {
		drawEmptySpace(canvas, startXY.x, startXY.y, width, height);
	}

	/**
	 * Draws a home space on the board.
	 * @param canvas the canvas to draw in
	 * @param startXY the start x/y location
	 * @param width the width of the space
	 * @param height the height of the space
	 * @param color the color of the space
	 */
	public void drawHomeSpace(Canvas canvas, Point startXY, int width,
			int height, int color) {
		drawHomeSpace(canvas, startXY.x, startXY.y, width, height, color);
	}

	/**
	 * Draws a safe space on the board.
	 * @param canvas the canvas to draw in
	 * @param startXY the start x/y location
	 * @param width the width of the space
	 * @param height the height of the space
	 * @param color the color of the space
	 */
	public void drawSafeSpace(Canvas canvas, Point startXY, int width,
			int height, int color) {
		drawSafeSpace(canvas, startXY.x, startXY.y, width, height, color);
	}

	/**
	 * Draws a start space on the board.
	 * @param canvas the canvas to draw in
	 * @param startXY the start x/y location
	 * @param width the width of the space
	 * @param height the height of the space
	 * @param color the color of the space
	 */
	public void drawStartSpace(Canvas canvas, Point startXY, int width,
			int height, int color) {
		drawStartSpace(canvas, startXY.x, startXY.y, width, height, color);
	}

	/**
	 * Draws the board background.
	 * @param canvas the canvas to draw in
	 * @param startX the start x location
	 * @param startY the y location
	 * @param width the width of the board background
	 * @param height the height of the board background
	 */
	protected abstract void drawBackground(Canvas canvas, int startX,
			int startY, int width, int height);

	/**
	 * Draws the discard rectangle on the board.
	 * @param canvas the canvas to draw in
	 * @param startX the start x location
	 * @param startY the start y location
	 * @param width the width of the discard rectangle
	 * @param height the height of the discard rectangle
	 */
	protected abstract void drawDiscardRectangle(Canvas canvas, int startX,
			int startY, int width, int height);

	/**
	 * Draws an empty space on the board.
	 * @param canvas the canvas to draw in
	 * @param startX the start x location
	 * @param startY the start y location
	 * @param width the width of the space
	 * @param height the height of the space
	 */
	protected abstract void drawEmptySpace(Canvas canvas, int startX,
			int startY, int width, int height);

	/**
	 * Draws a home space on the board.
	 * @param canvas the canvas to draw in
	 * @param startX the start x location
	 * @param startY the start y location
	 * @param width the width of the space
	 * @param height the height of the space
	 * @param color the color of the space
	 */
	protected abstract void drawHomeSpace(Canvas canvas, int startX,
			int startY, int width, int height, int color);

	/**
	 * Draws a safe space on the board.
	 * @param canvas the canvas to draw in
	 * @param startX the start x location
	 * @param startY the start y location
	 * @param width the width of the space
	 * @param height the height of the space
	 * @param color the color of the space
	 */
	protected abstract void drawSafeSpace(Canvas canvas, int startX,
			int startY, int width, int height, int color);

	/**
	 * Draws a start space on the board.
	 * @param canvas the canvas to draw in
	 * @param startX the start x location
	 * @param startY the start y location
	 * @param width the width of the space
	 * @param height the height of the space
	 * @param color the color of the space
	 */
	protected abstract void drawStartSpace(Canvas canvas, int startX,
			int startY, int width, int height, int color);

}
